#include <iostream>
#include <pthread.h>
#include <unistd.h>

using std::cout;
using std::endl;

void clean_func(void *p)
{
    free(p);
    printf("clean_func finish\n");
}

void *func(void *p)
{
    char *q = (char *)malloc(20);
    // 设置清理函数
    pthread_cleanup_push(clean_func, q);

    pthread_cleanup_pop(1);
    return nullptr;
}

int main(int args, char *argv[])
{
    pthread_t thid;
    pthread_create(&thid, nullptr, func, nullptr);

    pthread_join(thid, nullptr);
    return 0;
}